from api.base import Base
from common.creat_singlelogin_data import CreatSingleloginData
from common.get_log import log
from common.mysql import Mysql1, Mysql2
import pytest
from common.read_config import ReadOtherConfig




def pytest_collection_modifyitems(items):
    """
    支持读取的jsoncase文件中的中文字符
    给收集到的用例按ASCII码排序
    """
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')


@pytest.fixture(scope="session", autouse=True)
def pro_beforecase():
    """
    此方法为整个项目前置条件
    查询单点登录数据库是否已经有创建好的市级,县级和学校(通过数据库判断)
    """
    log.info('创建整个项目的前置条件,查询单点登录数据库是否已经有创建好的市级,县级和学校,如果有则跳过,如果没有就开始创建')
    mysql1 = Mysql1()
    creatdata = CreatSingleloginData()

    # 查询在单点登录系统中市级是否存在
    result1 = mysql1.select('select * from t_auth_school where name="林芝市"')
    if result1 == None:  # 不存在则创建
        log.info('单点登录市级账号不存在,现在开始创建')
        creatdata.creat_singlelogin_school(1, "林芝市", "http://goodgoodstudy", "林芝市", 1001, None)
    else:
        log.info("单点登录市级账号已经存在,无需创建")

    # # 查询单点登录县级是否存在
    # result2 = mysql1.select('select * from t_auth_school where name="米林县"')
    # if result2 == None:  # 不存在则创建
    #     log.info('单点登录县级账号不存在,现在开始创建')
    #     creatdata.creat_singlelogin_school(1, "米林县", "http://goodgoodstudy", "米林县", 1002, 1001)
    # else:
    #     log.info("单点登录县级账号已经存在,无需创建")
    #
    # # 查询单点登录学校是否存在
    # result3 = mysql1.select('select * from t_auth_school where name="米林一中"')
    # result4 = mysql1.select('select * from t_auth_school where name="米林二中"')
    # result5 = mysql1.select('select * from t_auth_school where name="米林三中"')
    # result6 = mysql1.select('select * from t_auth_school where name="米林四中"')
    # if result3 == None or result4==None or result5==None or result6==None:  # 不存在则创建
    #     log.info('单点登录学校不存在,现在开始创建')
    #     creatdata.creat_singlelogin_school(0, "米林县", "http://goodgoodstudy", "米林一中", 1003, 1002)
    #     creatdata.creat_singlelogin_school(0, "米林县", "http://goodgoodstudy", "米林二中", 1004, 1002)
    #     creatdata.creat_singlelogin_school(0, "米林县", "http://goodgoodstudy", "米林三中", 1005, 1002)
    #     creatdata.creat_singlelogin_school(0, "米林县", "http://goodgoodstudy", "米林四中", 1006, 1002)
    # else:
    #     log.info("单点登录学校账号已经存在,无需创建")
    # log.info("项目前置条件创建完毕,开始执行测试用例")

    yield
    log.info("项目所有测试用例均已执行完毕")

@pytest.fixture(scope="module", autouse=True)
def update_name():
    """
    此方法为更新电子班牌市级名称
    :return:
    """
    mysql2 = Mysql2()
    mysql2.update('update user set name="林芝市" where name="admin"')
    log.info("市级名称已更新为林芝市")

@pytest.fixture(scope="module", autouse=True)
def citytoken():
    """
    此方法为获取电子班牌市级账号的token
    :return:
    """
    token = Base().get_token(
        ReadOtherConfig().get_config_value('account_info', 'city_username'),
        ReadOtherConfig().get_config_value('account_info', 'city_password'))
    return token


